#leetcode题目71：简化路径
#难度：中等

#方法：栈

class Solution:
    def simplifyPath(self, path: str) -> str:
        names = path.split('/')
        stack = []
        for name in names:
            if name == '..':
                if stack:
                    stack.pop()
            elif name and name != '.':
                stack.append(name)
        return '/' + '/'.join(stack)


#测试数据
path="/home/"
#预期输出："/home"
solution=Solution()
print(solution.simplifyPath(path))

path="/../"
#预期输出："/"
solution=Solution()
print(solution.simplifyPath(path))

path="/home//foo/"
#预期输出："/home/foo"
solution=Solution()
print(solution.simplifyPath(path))

path="/a/./b/../../c/"
#预期输出："/c"
solution=Solution()
print(solution.simplifyPath(path))